#include "Track.h"
#include "AudioUtils.h"
using namespace AudioModel;

Track::~Track(){}
Track::Track(FMOD::System * system)
{
	gain=0;
	pan=0;
	mute=false;
	record=false;
	solo=false;
	this->system=system;
	system->createChannelGroup("Input1",&InputChannel);
	system->createChannelGroup("Output1",&OutputChannel);

	plugManager = new PlugManager::PluginManager(system,OutputChannel);

	this->label="AudioTrack -";

	for(int i=0;i<8;i++){
		clips[i].channel=NULL;
		clips[i].clip=NULL;
		clips[i].index=NULL;
	}
}
Track::Track(const Track & track){
	this->gain=track.gain;
	this->pan=track.pan;
	this->InputChannel=track.InputChannel;
	this->OutputChannel=track.OutputChannel;
	this->mute=track.mute;
	this->record=track.record;
	this->solo=track.solo;
	this->system=track.system;
	this->label=track.label;
	this->listen=track.listen;

}
Track & Track::operator=(const Track& track){
	this->gain=track.gain;
	this->pan=track.pan;
	this->InputChannel=track.InputChannel;
	this->OutputChannel=track.OutputChannel;
	this->mute=track.mute;
	this->record=track.record;
	this->solo=track.solo;
	this->system=track.system;
	this->label=track.label;
	this->listen=track.listen;
	return *this;
}

void Track::addClip(AudioClip * clip, unsigned int start_offset,unsigned int stop_offset,unsigned int clip_len,unsigned int track_offset,int clipNum){
	if(clipNum>=0 && clipNum<8){
		clips[clipNum].clip=clip;
		clips[clipNum].start_offset=start_offset;
		clips[clipNum].stop_offset=stop_offset;
	}
}
void Track::rmClip(int clipIndex){
	if(clipIndex>0 && clipIndex<9){
		clips[clipIndex-1].clip->getSound()->release();
	}
}
void Track::play(){
	
}
void Track::pause(){
}
void Track::resume(){
}
void Track::stop(){
	for(unsigned int *i=0;*i<8;i++)
	{
		bool *isPlaying=false;
		int *numChannels=0;
		OutputChannel->getNumChannels(numChannels);
		for(int j=0;j<*numChannels;i++)
		{
			FMOD::Channel *chan;
			OutputChannel->getChannel(j,&chan);
			chan->isPlaying(isPlaying);
			if(*isPlaying){
				chan->stop();
			}
		}
	}
}

bool Track::isMute(){
	return mute;
}
bool Track::isSolo(){
	return solo;
}
bool Track::isRecording(){
	return false;
}

float Track::getGain(){
	return 0;
}
float Track::getPan(){
	return 0;
}
string Track::getLabel(){
	return label;
}

void Track::setGain(float g){
	OutputChannel->setVolume(g);
}
void Track::setPan(float p){
	FMOD::Channel *chan;
	for(int i=0;i<8;i++){
		OutputChannel->getChannel(i,&chan);
		chan->setPan(p);
	}
}
void Track::setLabel(string label){
}

void Track::setInputChannel(FMOD::ChannelGroup *){
}
void Track::setOutputChannel(FMOD::ChannelGroup * outputChannel){
	this->OutputChannel=outputChannel;
}

FMOD::ChannelGroup * Track::getInputChannel(){
	return InputChannel;
}
FMOD::ChannelGroup * Track::getOutputChannel(){
	return OutputChannel;
}

void Track::setMute(){
	OutputChannel->setMute(true);
	mute=true;
}
void Track::setUnMute(){
	OutputChannel->setMute(false);
	mute=false;
}
void Track::setSolo(){
	solo=true;
}
void Track::setUnSolo(){
	solo=false;
}
void Track::setListen(bool listen){
	this->listen=listen;
}
void Track::setRec(bool rec){
	this->record=rec;
}

AudioClip_pos * Track::getClips(){
	return clips;
}

FMOD::DSP * Track::getDSP(FMOD_DSP_TYPE type){
	return plugManager->getBuiltInDsp(type);
}

bool Track::isPlaying(int clipID){
	bool on=false;
	FMOD_RESULT result=clips[clipID].channel->isPlaying(&on);
	AudioUtils::ERRCHECK(result);
	return on;
}

void Track::stopChannel(int clipID){
	clips[clipID].channel->stop();
}

bool Track::isAssign(int clipID){
	if(clips[clipID].clip!=NULL)
		return true;
	return false;
}